package com.py.credit.controller;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.service.CreditAppService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 异议处理controller
 */
@RestController
@RequestMapping(value = "/creditApp")
public class CreditAppController {

    private final static String FRONT_TABLE_ID = "creditApp";

    @Autowired
    private CreditAppService creditAppService;

    @Autowired
    private RemoteBaseService remoteBaseService;

    @Value("${absolutepath}")
    private String absolutepath;


    /**
     * 异议修改分页列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/collectionByDeptTop5.json")
    public RestServiceResult<List<Map>> collectionByDeptTop5(HttpServletRequest request) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("bigScreenForBlackAndRedService#getList", "未获取到当前用户信息！");
                return result;
            }

            result.setData(creditAppService.collectionByDeptTop5());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;

        }
    }

    /**
     * 异议修改分页列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/dataQualityTop10.json")
    public RestServiceResult<List<Map>> dataQualityTop10(HttpServletRequest request) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        //creditAppService.initDeptScore();
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("bigScreenForBlackAndRedService#getList", "未获取到当前用户信息！");
                return result;
            }

            result.setData(creditAppService.dataQualityTop10());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;

        }
    }

    /**
     * 异议修改分页列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/mapAnalyse.json")
    public RestServiceResult<List<Map>> mapAnalyse(HttpServletRequest request, String qybm) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("bigScreenForBlackAndRedService#getList", "未获取到当前用户信息！");
                return result;
            }

            result.setData(creditAppService.mapAnalyse(qybm));
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;

        }
    }

    /**
     * 异议修改分页列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/collectionByAreaTop5.json")
    public RestServiceResult<List<Map>> collectionByAreaTop5(HttpServletRequest request) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("bigScreenForBlackAndRedService#getList", "未获取到当前用户信息！");
                return result;
            }

            result.setData(creditAppService.collectionByAreaTop5());
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;

        }
    }

    /**
     * 异议修改分页列表
     *
     * @param page
     * @return
     */
    @IgnoreAuthority
    @RequestMapping(value = "/checkAnalyse.json")
    public RestServiceResult<List<Map>> checkAnalyse(HttpServletRequest request) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        //RestServiceResult<List<DictEntity>> codesData = remoteBaseService.getDictByDictKey("HCLX");

        SysUser userInfo = null;
//        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
//            userInfo = user.getData();
//            if (null == userInfo || userInfo.getId() <= 0) {
//                result.setCode(RestServiceResult.CODE_UNKNOW);
//                result.setMsg("系统异常，请稍后再试！");
//                ExceptionLogger.error("bigScreenForBlackAndRedService#getList", "未获取到当前用户信息！");
//                return result;
//            }
//            /*List<DictEntity> listCodes=codesData.getData();*/
//            result.setData(creditAppService.checkAnalyse());
//            return result;
//        } else {
//            result.setCode(user.getCode());
//            result.setMsg(user.getMsg());
//            return result;
//
//        }
        result.setData(creditAppService.checkAnalyse());
        return result;
    }

    /**
     * 异议修改分页列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/selectTop5Radar.json")
    public RestServiceResult<List<Map>> selectTop5Radar(HttpServletRequest request, String qybm) {
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();

        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("bigScreenForBlackAndRedService#getList", "未获取到当前用户信息！");
                return result;
            }
            result.setData(creditAppService.selectTop5Radar(qybm));
            return result;
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
    }

    /**
     * 功能描述: 异议申请和信用修复按月统计
     *
     * @return
     * @author YaoS
     * @date 19/7/1 15:55
     */
    @IgnoreAuthority
    @RequestMapping(value = "/applicationAndCreditRepairCountByMonth.json")
    public RestServiceResult<Map> applicationAndCreditRepairCountByMonth() {
        return new RestServiceResult<>(creditAppService.applicationAndCreditRepairCountByMonth());

    }


    /**
     * @api {get} /creditMap/initDeptMap.json 部门汇总数据初始化定时任务
     * @apiName initDeptMap
     * @apiVersion 0.0.1
     * @apiGroup 部门汇总数据初始化
     */
    @RequestMapping(value = "/initDeptScore.do")
    public RestServiceResult<Object> initDeptScore() {
        try {
            creditAppService.initDeptScore();
            return new RestServiceResult<Object>();
        } catch (Exception e) {
            RestServiceResult<Object> result = new RestServiceResult<Object>();
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("任务提醒定时任务异常！ERROR:" + e.getMessage());
            ExceptionLogger.error("任务提醒定时任务DataRemindController#taskRemind", "任务提醒定时任务异常！ERROR:" + e.getMessage(), e);
            return result;
        }
    }
}